package com.secsbrain.cmdb.job;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.secsbrain.cmdb.biz.IIpService;
import com.secsbrain.cmdb.web.dto.IpTest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * 定时检查IP信息
 *
 * @author zhaoyi
 */
@Component("checkIpJob")
public class CheckIpJob {
    
    private static final Logger logger = LoggerFactory.getLogger(CheckIpJob.class);

    @Autowired
    private IIpService ipService;
    
    @Value("${cmdb.offline.agent.url:}")
	private String offlineAgentUrl;
	
	@Value("${cmdb.online.agent.url:}")
	private String onlineAgentUrl;
    
    public void checkIp(){
    	try {
    		if(StringUtils.isNotEmpty(offlineAgentUrl)){
        		String hostRes = new RestTemplate().getForObject(offlineAgentUrl + "/ip/testAll", String.class);
        		logger.debug(hostRes);
        		List<IpTest> list = JSON.parseArray(hostRes, IpTest.class);
        		ipService.updateIp(list);
        	}
        	
        	if(StringUtils.isNotEmpty(onlineAgentUrl)){
        		String hostRes = new RestTemplate().getForObject(onlineAgentUrl + "/ip/testAll", String.class);
        		List<IpTest> list = JSON.parseArray(hostRes, IpTest.class);
        		ipService.updateIp(list);
        	}
        	
		} catch (Exception e) {
			logger.error("",e);
		}
    }
    
}
